package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 第二产业结构变动的反应弹性同比评估指标计算类.
 */
public class SecondISCReactionElasticityLastCalculateService extends IndicatorEvaluationCalculateService {

	private String BaseIndicatorName = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private String BasePersonName = ProvinceIndicatorConstant.OCEAN_PERSON_NAME;
	private String secondRateIndicatorName = ProvinceIndicatorConstant.SECOND_RATE_INDICATOR_NAME;
	private String FirstName = ProvinceIndicatorConstant.SECOND_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			return calculateResult(indicatorAccountVO, ProvinceIndicatorConstant.JS_POPULATION_NAME);
		} else {
			return calculateResult(indicatorAccountVO, BasePersonName);
		}

	}

	private Double calculateResult(IndicatorAccountVO indicatorAccountVO, String populationIndicatorName) {
		// 同比年份
		int fixBaseYear = indicatorAccountVO.getYear() - 1;
		Double oceanBase = getBaseIndicatorVal(indicatorAccountVO, fixBaseYear, BaseIndicatorName);
		Double personBase = getBaseIndicatorVal(indicatorAccountVO, fixBaseYear, populationIndicatorName);
		Double oceanCurrent = getBaseIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), BaseIndicatorName);
		Double personCurrent = getBaseIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(),
				populationIndicatorName);
		Double r = Double.valueOf(0);
		// if (personBase.equals(Double.valueOf(0)) || oceanBase.equals(Double.valueOf(0))
		// || personCurrent.equals(Double.valueOf(0))) {
		// r = Double.valueOf(0);
		// } else {
		Validate.isTrue(!(Double.valueOf(0).equals(personBase)));
		Validate.isTrue(!(Double.valueOf(0).equals(oceanBase)));
		Validate.isTrue(!(Double.valueOf(0).equals(personCurrent)));
		r = ((oceanCurrent / personCurrent) - (oceanBase / personBase)) / (oceanBase / personBase);
		// }
		Double firstBase = geFirstIndicatorVal(indicatorAccountVO, fixBaseYear, FirstName);

		Validate.isTrue(!(Double.valueOf(0).equals(oceanBase)));
		// 同比第二产业比重
		Double firstBasetoGOP = oceanBase == 0 ? 0 : (firstBase / oceanBase) * 100;
		// 当期第二产业比重
		Double firstCurrent = getSecondIndustryRate(indicatorAccountVO, secondRateIndicatorName);

		Validate.isTrue(!(Double.valueOf(0).equals(firstBasetoGOP)));
		Double a = firstBasetoGOP == 0 ? 0 : firstCurrent / firstBasetoGOP;
		return r == 0 ? a : a + (a - 1) / r;
	}

	private Double geFirstIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year, String name) {
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO, year, name);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	private Double getBaseIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year, String name) {
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO, year, name);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	private Double getSecondIndustryRate(IndicatorAccountVO indicatorAccountVO, String name) {
		Integer indicatorId = getIndicatorEvaluationId(name, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Double evaluationIndicatorValue = getEvaluationIndicatorValue(cri);
		Validate.isTrue(evaluationIndicatorValue != null);
		return evaluationIndicatorValue;
	}

}
